{{$api_id = $api.id}}
<form role="form" id="js_addApiForm" action="{{url('api/add')}}" method="post">

    <input name="api[id]" type="hidden" value="{{$api_id}}">
    <div class="row">
        <div class="col-lg-12">
            <div class="page-header">
                <h1>接口编辑</h1>
                <div class="opt-btn">
                    <a class="btn btn-sm btn-success js_submit" href="javascript:void(0);"><i class="fa fa-fw fa-save"></i>保存</a>
                    <a class="btn btn-sm btn-warning" href="javascript:location.reload();"><i class="fa fa-fw fa-reply"></i>取消</a>

                </div>
            </div>
        </div>
        <!-- /.col-lg-12 -->
    </div>


    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    接口详情
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-12">

                                <div class="form-group">
                                    <label>接口名称</label>
                                    <input class="form-control" name="api[title]" value="{{$api.title}}" placeholder="必填" datatype="*2-250" nullmsg="请输入网站名称！" errormsg="至少2个字符">
                                </div>

                                <div class="form-group">
                                    <label>所属分类</label>
                                    <select class="form-control" name="api[module_id]">
                                        {{foreach $modules as $module}}
                                        <option value="{{$module.id}}" {{if $module.id == $api.module_id}}selected{{/if}}>{{$module.title}}</option>
                                        {{/foreach}}
                                    </select>
                                </div>

                                <div class="form-group">
                                    <label class="control-label">请求类型</label>
                                    <div class="form-group">
                                        {{foreach \app\api::get_method_list() as $k => $method}}
                                        <label class="radio-inline">
                                            <input type="radio" name="api[method]" value="{{$k}}" {{if $k == $api.method}}checked{{/if}}> {{$method}}
                                        </label>
                                        {{/foreach}}

                                    </div>
                                </div>

                                <div class="form-group">
                                    <label>接口路径

                                    </label>

                                    <input class="form-control" name="api[uri]" value="{{$api.uri}}" placeholder="必填，不包含域名部分" datatype="/^(?!http://).*$/" nullmsg="请输入网站名称！" errormsg="至少2个字符">

                                </div>


                                <div class="form-group">
                                    <label>接口描述</label>
                                    <textarea class="form-control" name="api[intro]" rows="3" placeholder="非必填" >{{$api.intro}}</textarea>
                                </div>

                        </div>
                        <!-- /.col-lg-6 (nested) -->

                        <!-- /.col-lg-6 (nested) -->
                    </div>

                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
    </div>

    <!-- /.row -->
    <!-- /.row -->

    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    请求参数
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="table-responsive">

                        <div class="panel-request">
                            {{include_file name='field/request/load'}}
                        </div>

                        <div class="form-group">
                            <button type="button" class="btn btn-default js_addRequestFieldBtn" data-title="添加请求参数" data-id="{{$api.id}}-0"><i class="fa fa-fw fa-plus"></i>添加参数</button>
                        </div>
                    </div>
                    <!-- /.table-responsive -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-6 -->
    </div>


    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    返回参数
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="table-responsive">
                        <div class="panel-response">
                            {{include_file name='field/response/load'}}
                        </div>

                        <div class="form-group">
                            <button type="button" class="btn btn-default js_addResponseFieldBtn" data-title="添加响应参数"  data-id="{{$api.id}}-0-0"><i class="fa fa-fw fa-plus"></i>添加参数</button>
                        </div>
                    </div>
                    <!-- /.table-responsive -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-6 -->
    </div>


    <!-- /.row -->

    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    返回示例
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <textarea class="form-control" name="api[demo]" rows="3" placeholder="非必填，返回示例必须是合法json格式(键值对都是双引号)">{{$api.demo}}</textarea>
                    <!-- /.table-responsive -->
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-6 -->
    </div>
</form>

<!-- 添加/编辑请求字段模态框 -->
<div class="modal fade" id="js_addRequestFieldModal" role="dialog">
    <div class="modal-dialog" role="document">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加请求参数</h4>
            </div>
            <div class="modal-body">

                <iframe id="js_addRequestFieldIframe" style="min-height: 450px;" src="{{url('field/request')}}"></iframe>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default js_close" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary js_requestSubmitBtn">提交</button>
            </div>

        </div>

    </div>

</div>
<!-- 添加/编辑响应字段模态框 -->
<div class="modal fade" id="js_addResponseFieldModal" role="dialog">
    <div class="modal-dialog" role="document">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">添加响应参数</h4>
            </div>
            <div class="modal-body">

                <iframe id="js_addResponseFieldIframe" style="min-height: 320px;" src="{{url('field/response')}}"></iframe>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default js_close" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary js_responSubmitBtn">提交</button>
            </div>

        </div>

    </div>

</div>

<script>
    $(function(){
        //control+s快速保存
        document.onkeydown=function()   {
            if (event.ctrlKey == true && event.keyCode == 83) {//Ctrl+S
                event.returnvalue = false;
                $("#js_addApiForm").submit();
                //alert('触发ctrl+s');
            }

        };

        //表单合法性验证
        $("#js_addApiForm").validateForm();

        // 添加/编辑请求字段
        $(".js_addRequestFieldBtn").iframeModal({
            modalItem: '#js_addRequestFieldModal', //modal元素
            iframeItem: '#js_addRequestFieldIframe', //提交按钮
            submitBtn: '.js_requestSubmitBtn',
            clickBtn: '.js_addRequestFieldBtn',
        });

        // 添加/编辑响应字段
        $(".js_addResponseFieldBtn").iframeModal({
            modalItem: '#js_addResponseFieldModal', //modal元素
            iframeItem: '#js_addResponseFieldIframe', //提交按钮
            submitBtn: '.js_responSubmitBtn',
            clickBtn: '.js_addResponseFieldBtn',
        });

        //删除字段
        $(document).delegate('.js_deleteFieldBtn', 'click',function(event){
            // 阻止事件冒泡
            event.stopPropagation();
            var thisObj = $(this);

            var id = $(this).data('id');

            var url = "{{url('field/delete')}}";

            confirm('确认删除该参数?', function(){

                $.post(url, { id:id }, function(json){

                    if(json.code == 200){

                        alert(json.msg, 500, function () {
                            //移除tr
                            thisObj.closest('tr').remove();
                        });

                    }else{

                        alert(json.msg, 2000);

                    }

                }, 'json');
            });

        });

    });

</script>